package com.hxkj.dao.impl;

import com.hxkj.dao.AuthUserDao;
import com.hxkj.domain.po.AuthRole;
import com.hxkj.domain.po.AuthUserAgent;
import com.hxkj.domain.po.LinkUser;
import com.hxkj.mapper.AuthRoleMapper;
import com.hxkj.mapper.AuthUserMapper;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
* 数据操作实现
*
* @author: chenwei
* @version: 1.0
* @datetime: 2018-05-21 17:59:26
*
*/
@Service("authUserDao")
public class AuthUserDaoImpl implements AuthUserDao {

    @Autowired
    private AuthUserMapper authUserMapper;

    @Resource
    private AuthRoleMapper authRoleMapper;
    @Override
    public AuthUser get(Long userId) {
        return authUserMapper.selectByPrimaryKey(userId);
    }


    @Override
    public List<AuthUser> getUserList(AuthUser authUser) {
        return authUserMapper.getUserList(authUser);
    }


    public List<AuthUser> getUserListAsSite(AuthUser authUser) {
        return  authUserMapper.getUserListAsSite(authUser);
    }

    @Override
    public List<AuthUser> getUserByName(AuthUser authUsers) {
        return authUserMapper.getUserByName(authUsers);
    }

    @Override
    public AuthUser getUserByUserId(Long userId) {
        return authUserMapper.getUserByUserId(userId);
    }



    @Override
    public List<AuthRole> getAuthRoleByUser(AuthUser authUser) {
        return authUserMapper.getAuthRoleByUser(authUser);
    }

    @Override
    public AuthUser getUserPassword(Long userId) {
        return authUserMapper.getUserPassword(userId);
    }

    @Override
    public int delete(Long userId) {
        return authUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int delete(AuthUser authUser) {
        return authUserMapper.delete(authUser);
    }

    public int save(AuthUser authUser) throws Exception {
        //调用MD5工具类对密码加密
        authUser.setSalt(UUID.randomUUID().toString());//添加盐标签
        String password = MD5Utils.getEncryptedPwd(authUser.getPassword(), authUser.getCredentialsSalt());
        authUser.setPassword(password);
        //设置状态
        if (authUser.getUserStatus()==null) {
            authUser.setUserStatus((byte) 0);//0为启用,1为停用
        }
        //更新时间戳
        authUser.setCreateTime(System.currentTimeMillis());
        authUser.setUpdateTime(System.currentTimeMillis());
        return authUserMapper.insert(authUser);
    }

    @Override
    public int update(AuthUser authUser) throws Exception{
        if (StringUtils.isNotBlank(authUser.getPassword())) {
            //调用MD5工具类对密码加密
            String password = MD5Utils.getEncryptedPwd(authUser.getPassword(), authUser.getCredentialsSalt());
            authUser.setPassword(password);
        }
        //更新当前时间戳
        authUser.setUpdateTime(System.currentTimeMillis());
        return authUserMapper.update(authUser);
    }

    /**
     * 更新用户(更改Roletag字段)
     * @param authUser
     * @return
     * @throws Exception
     */
    @Override
    public int updateUser(AuthUser authUser) throws Exception{

        return authUserMapper.update(authUser);
    }

    @Override
    public List<AuthUser> findUserByAuthRole(AuthRole role) throws Exception {
        return authUserMapper.findUserByAuthRole(role);
    }

    @Override
    public List<AuthRole> findAuthRoleByUser(Long userId) throws Exception {
        return authRoleMapper.findAuthRoleByUser(userId);
    }

    @Override
    /**
     * 查询所有关联了角色的用户
     * @return
     */
    public List<AuthUser> findRoleUser() {
        return authUserMapper.findRoleUser();
    }

    @Override
    /**
     * 查询所有未关联任何角色的用户
     * @return
     */
    public List<AuthUser> findNoRoleUser(AuthRole role) {
        return authUserMapper.findNoRoleUser(role);
    }

    @Override
    public List<AuthUser> getUser(String keyword) {
        return authUserMapper.findUserByLike(keyword);
    }

    @Override
    public void addAuthRoleToUser(Long uid, List<AuthRole> roles) throws Exception {
        authUserMapper.addAuthRoleToUser(uid,roles);
    }

    @Override
    public void deleteAuthRoleByUser(Long uid, List<AuthRole> roles) throws Exception {
        authUserMapper.deleteAuthRoleByUser(uid,roles);
    }

    /**
     * 根据自媒体角色获取所有的自媒体业务员账号
     * @param authUserAgent
     * @return
     */
    @Override
    public List<AuthUserAgent> getAgentList(AuthUserAgent authUserAgent) {
        return authUserMapper.getAgentList(authUserAgent);
    }
}
